home *** CD-ROM | disk | FTP | other *** search
/ Dr. Windows 3 / dr win3.zip / dr win3 / WINPROGS / WTJ208.ZIP / MILLER / CGWINDOW.CPP next >
C/C++ Source or Header  |  1993-04-11  |  14KB  |  453 lines

  1. //----------------------------------------------------------------------
  2. // File: CGWINDOW.CPP
  3. //----------------------------------------------------------------------
  4. #pragma hdrstop
  5. #include <owl.h>
  6. #include <button.h>
  7. #include <checkbox.h>
  8. #include <groupbox.h>
  9. #include <radiobut.h>
  10. #include <static.h>
  11. #include <combobox.h>
  12. #include <edit.h>
  13. #include <scrollba.h>
  14. #include <btree.h>
  15. #include <stdtempl.h>
  16. #include "cgwindow.h"
  17.  
  18. //----------------------------------------------------------------------
  19. // GLOBAL VARIABLES
  20. //----------------------------------------------------------------------
  21. Btree registerWindowTypes;
  22. PTGroupBox activeGroup;
  23.  
  24. static char    xxButton  = BUTTONCLASS,
  25.           xxEdit    = EDITCLASS,
  26.           xxStatic  = STATICCLASS,
  27.           xxListBox = LISTBOXCLASS,
  28.           xxScrollBar    = SCROLLBARCLASS,
  29.           xxComboBox     = COMBOBOXCLASS;
  30.  
  31. //----------------------------------------------------------------------
  32. // Registration of predefined controls
  33. //----------------------------------------------------------------------
  34. CGWindowRegister regButton(&xxButton, BuildButton);
  35. CGWindowRegister regEdit(&xxEdit, BuildEdit);
  36. CGWindowRegister regStatic(&xxStatic, BuildStatic);
  37. CGWindowRegister regListBox(&xxListBox, BuildListBox);
  38. CGWindowRegister regScrollBar(&xxScrollBar, BuildScrollBar);
  39. CGWindowRegister regComboBox(&xxComboBox, BuildComboBox);
  40.  
  41. //----------------------------------------------------------------------
  42. // Class: CGWindow
  43. //----------------------------------------------------------------------
  44. CGWindow::CGWindow(PTWindowsObject AParent, LPSTR ATitle)
  45.      : TWindow(AParent, ATitle) {
  46.  
  47.      // Allow keyboard controls
  48.      EnableKBHandler();
  49.  
  50.      // Initialize scrolling
  51.      Attr.Style |= WS_HSCROLL | WS_VSCROLL;
  52.      Scroller = new TScroller(this, 1, 1, 0, 0);
  53.      Scroller->XLine = Scroller->YLine = 20;
  54.  
  55.      // Store resource name
  56.      if(ATitle)
  57.           AResourceName = strdup(ATitle);
  58.  
  59.      hFont = 0;
  60.  
  61. } // F: CGWindow::CGWindow
  62.  
  63. //----------------------------------------------------------------------
  64. // F: CGWindow::~CGWindow
  65. //----------------------------------------------------------------------
  66. CGWindow::~CGWindow() {
  67.  
  68.      // Delete Font
  69.      if (hFont)
  70.           DeleteObject(hFont);
  71.  
  72.      // Delete resource name
  73.      if (AResourceName)
  74.           delete AResourceName;
  75.  
  76. } // F: CGWindow::~CGWindow
  77.  
  78. //----------------------------------------------------------------------
  79. // F: CGWindow::CreateFont
  80. //----------------------------------------------------------------------
  81. HFONT CGWindow::CreateFont(LPSTR lpszFaceName, int
  82. nPointSize) {
  83.  
  84.      LOGFONT   lf;
  85.      HDC       hDC;
  86.  
  87.      memset(&lf, 0, sizeof(LOGFONT));
  88.      hDC = GetDC(NULL);
  89.      lf.lfHeight = -MulDiv(nPointSize, GetDeviceCaps(hDC,
  90. LOGPIXELSY),        72);
  91.      ReleaseDC(NULL, hDC);
  92.      lf.lfWeight = FW_BOLD;
  93.      lf.lfQuality = PROOF_QUALITY;
  94.      lf.lfPitchAndFamily = FF_DONTCARE;
  95.      strcpy(lf.lfFaceName, lpszFaceName);
  96.      return CreateFontIndirect(&lf);
  97.  
  98. } // F: CGWindow::CreateFont
  99.  
  100. //----------------------------------------------------------------------
  101. // F: CGWindow::PreProcessControl
  102. //----------------------------------------------------------------------
  103. void CGWindow::PreProcessControl(DLGITEM & ADlgItem) {
  104.  
  105.      // Scale Coordinates
  106.      ADlgItem.X  = MulDiv(ADlgItem.X,  nScaleX, 4);
  107.      ADlgItem.Y  = MulDiv(ADlgItem.Y,  nScaleY, 8);
  108.      ADlgItem.CX = MulDiv(ADlgItem.CX, nScaleX, 4);
  109.      ADlgItem.CY = MulDiv(ADlgItem.CY, nScaleY, 8);
  110.      if((ADlgItem.Style & WS_GROUP) == WS_GROUP)
  111.           activeGroup = NULL;
  112. } // F: CGWindow::PreProcessControl
  113.  
  114. //----------------------------------------------------------------------
  115. // F: CGWindow::ProcessControl
  116. //----------------------------------------------------------------------
  117. PTWindowsObject CGWindow::ProcessControl(DLGITEM & DlgItem)
  118. {
  119.  
  120.      PTWindowsObject pAControl = NULL;
  121.  
  122.      // Construct Child Window
  123.      PCGWindowRegister reg =
  124. PCGWindowRegister(®isterWindowTypes.
  125.           findMember(CGWindowRegister(DlgItem.Class,
  126. NULL)));
  127.      if(reg != ZERO)
  128.           pAControl = reg->Build(this, DlgItem);
  129.  
  130.      return pAControl;
  131.  
  132. } // F: CGWIndow::ProcessControl
  133.  
  134.  
  135. //----------------------------------------------------------------------
  136. // F: CGWindow::PostProcessControl
  137. //----------------------------------------------------------------------
  138. void CGWindow::PostProcessControl(PTWindowsObject AControl,
  139. DLGITEM &      ADlgItem) {
  140.  
  141.      // Assign resource's style bits
  142.      PTWindow(AControl)->Attr.Style = ADlgItem.Style;
  143.  
  144. } // F: CGWIndow::PostProcessControl
  145.  
  146. //----------------------------------------------------------------------
  147. // F: CGWindow::SetupWindow
  148. //----------------------------------------------------------------------
  149. void CGWindow::SetupWindow() {
  150.      RECT      rect;
  151.      POINT          pt;
  152.      HANDLE    hMem;
  153.      LPSTR          lpDlg;
  154.      PDLGFONTHDR    pFontHdr = new DLGFONTHDR;
  155.      PDLGITEM  pDlgItem = new DLGITEM;
  156.      PDLGHDR   pDlgHdr = new DLGHDR;
  157.  
  158.      // Load & Lock Dialog Resource
  159.      hMem = LoadResource(GetModule()->hInstance,
  160.      FindResource(GetModule()->hInstance, AResourceName,
  161. RT_DIALOG));
  162.           lpDlg = LockResource(hMem);
  163.  
  164.      // Retrieve Dialog Header
  165.      lpDlg = GetDlgInfo((LPDLGTEMPLATE) lpDlg, pDlgHdr);
  166.  
  167.      // Retrieve Dialog Font Information
  168.      if (pDlgHdr->Style & DS_SETFONT) {
  169.           lpDlg = GetFontInfo(lpDlg, pFontHdr);
  170.           hFont = CreateFont(pFontHdr->szTypeFace,
  171.                pFontHdr->PointSize);
  172.      }
  173.  
  174.      // Scale Coordinates
  175.      TEXTMETRIC tm;
  176.      HDC hDC = GetDC(NULL);
  177.      HFONT hSaveFont = SelectObject(hDC, hFont);
  178.      GetTextMetrics(hDC, &tm);
  179.      SelectObject(hDC, hSaveFont);
  180.      ReleaseDC(NULL, hDC);
  181.  
  182.      nScaleX = tm.tmAveCharWidth + tm.tmOverhang;
  183.      nScaleY = tm.tmHeight;
  184.  
  185.      Place.left     = MulDiv(pDlgHdr->X,  nScaleX, 4);
  186.      Place.top = MulDiv(pDlgHdr->Y,  nScaleY, 8);
  187.      Place.right    = Attr.W = pDlgHdr->CX = MulDiv(pDlgHdr-
  188. >CX, nScaleX,       4);
  189.      Place.bottom = Attr.H = pDlgHdr->CY = MulDiv(pDlgHdr-
  190. >CY, nScaleY,       8);
  191.  
  192.      // Retrieve Dialog Items & Construct Child Windows
  193.      for (int i = 0; i < pDlgHdr->ItemCount; i++) {
  194.  
  195.           // Retrieve Dialog Item
  196.           lpDlg = GetDlgItem((LPDLGITEMTEMPLATE)lpDlg,
  197. pDlgItem);
  198.  
  199.           PreProcessControl(*pDlgItem);
  200.           PTWindowsObject pAControl =
  201. ProcessControl(*pDlgItem);
  202.           if(pAControl)
  203.                PostProcessControl(pAControl, *pDlgItem);
  204.  
  205.      } // for
  206.  
  207.      // Unlock & Free Resource
  208.      UnlockResource(hMem);
  209.      FreeResource(hMem);
  210.  
  211.      SetCaption(pDlgHdr->CaptionText);
  212.      TWindow::SetupWindow();
  213.      ForEach(SetControlFont, &hFont);
  214.  
  215.      GetWindowRect(HWindow, &rect);
  216.      pt.x = rect.left;
  217.      pt.y = rect.top;
  218.      ScreenToClient(Parent->GetClient() ?
  219.           Parent->GetClient()->HWindow :
  220.           Parent->HWindow, &pt);
  221.      MoveWindow(HWindow, pt.x, pt.y,
  222.           Place.right  + (GetSystemMetrics(SM_CXFRAME) * 2),
  223.           Place.bottom + (GetSystemMetrics(SM_CYFRAME) * 2)
  224. +
  225.                GetSystemMetrics(SM_CYCAPTION), FALSE);
  226.  
  227.      delete pFontHdr;
  228.      delete pDlgItem;
  229.      delete pDlgHdr;
  230.  
  231. } // F: CGWindow::SetupWindow
  232.  
  233. //----------------------------------------------------------------------
  234. // F: CGWindow::WMSize
  235. //----------------------------------------------------------------------
  236. void CGWindow::WMSize(RTMessage Msg) {
  237.  
  238.      TWindow::WMSize(Msg);
  239.      int nX = max(int(Scroller->YRange ?
  240.           Place.right - Msg.LP.Lo -
  241. GetSystemMetrics(SM_CXVSCROLL) :
  242.           Place.right - Msg.LP.Lo), 0);
  243.      int nY = max(int(Scroller->XRange ?
  244.           Place.bottom - Msg.LP.Hi -
  245. GetSystemMetrics(SM_CYHSCROLL) :
  246.           Place.bottom - Msg.LP.Hi), 0);
  247.      Scroller->SetRange(nX, nY);
  248.  
  249. } // F: CGWindow::WMSize
  250.  
  251. //----------------------------------------------------------------------
  252. // Class: CGWindowRegister
  253. //--------------------